<?php

/*
# ------------------------------------------------------------------------
# JA Job Board Package
# ------------------------------------------------------------------------
# Copyright (C) 2004-2010 JoomlArt.com. All Rights Reserved.
# @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
# Author: JoomlArt.com
# Websites: http://www.joomlart.com - http://www.joomlancers.com.
# ------------------------------------------------------------------------
*/
jimport( 'joomla.application.component.view' );

class JAJobBoardViewJAManageFields extends JAView
{
    /**
     * Display the view
     */
    function display($tmpl = null)
    {
		$task = JRequest::getVar("task", '');
		switch ($task){
			case 'add':
			case 'edit':				
				JAJobBoardViewJAManageFields::displayForm($tmpl);
				break;		
			default:
				JAJobBoardViewJAManageFields::displayListItems($tmpl);
		}
  	}
  	
  	
  	/**
  	* Display List of items
  	*/
  	function displayListItems($tmpl){  		
  		global $mainframe, $option;       
		$lists = $this->_getViewLists();
		$filter_table	= $mainframe->getUserStateFromRequest( $option.".jamanagefields.filter_table",	'filter_table',	'',	'word' );		
				
        // get data items
        $model = $this->getModel();
        $items = $model->getItems();
        $pagination = $model->_pagination;
        
        $ordering = true;
        $this->assignRef('ordering', $ordering);
        $this->assignRef('lists', $lists);
        $this->assignRef('items', $items);
        $this->assignRef('pagination', $pagination);
        parent::display($tmpl);
  	}
  	/**
  	* Display edit form
  	*/
  	function displayForm($tmpl){  	    
  		global $option, $mainframe;
  		
  	    $item = &$this->get('Item');
		$model = &$this->getModel();
  	    
		// Field Type List
	    $fieldTypeList = JobBoardConstant::getFieldTypeList();
	    $selectedType = is_null($item)?"" :  $item->field_type;
	    $disable = ($item->is_system)?'disabled="disabled"':'';
	    foreach($fieldTypeList as $fieldType=>$fieldTypeLabel){
            $fieldTypeHTML[] = JHTML::_('select.option',  $fieldType, $fieldTypeLabel );
		}
        $lists['field_type'] = JHTML::_('select.genericlist',   $fieldTypeHTML, 'field_type', $disable.' class="inputbox" size="5" style="width:200px"', 'value', 'text', $selectedType);
        
        // Field Form List
        $fieldFormList = JobBoardConstant::getFormList();
        $selectedForm = is_null($item)?"" :  $item->form_id;
        $fieldFormHTML[] = JHTML::_('select.option',  "", JText::_('Select Form') );
	    foreach($fieldFormList as $formId=>$formLabel){
            $fieldFormHTML[] = JHTML::_('select.option',  $formId, $formLabel );
		}
        $lists['form'] = JHTML::_('select.genericlist',   $fieldFormHTML, 'form_id', 'class="inputbox" size="1" '.$disable, 'value', 'text', $selectedForm);
		
        // Field Section List
        $fieldSectionList = $model->getFieldSectionList();
        $selectedSection = is_null($item)?"" :  $item->section;
	    foreach($fieldSectionList as $section=>$sectionLabel){
            $fieldSectionHTML[] = JHTML::_('select.option',  $section, $sectionLabel );
		}
        $lists['section'] = JHTML::_('select.genericlist',   $fieldSectionHTML, 'section', 'class="inputbox" size="1"', 'value', 'text', $selectedSection);
       	
        // Field Validation List
        $fieldValidationList = JobBoardConstant::getFieldValidationTypeList();   
        $selectedValidationType = is_null($item)?"" :  $item->field_validate_type;
	    foreach($fieldValidationList as $validationType=>$validationTypeLabel){
            $fieldValidationHTML[] = JHTML::_('select.option',  $validationType, $validationTypeLabel );
		}
        $lists['field_validate_type'] = JHTML::_('select.genericlist',   $fieldValidationHTML, 'field_validate_type', 'class="inputbox" size="1" onchange="return checkValidateType(this.value);"', 'value', 'text', $selectedValidationType);
		
		// Field Ordering
		if ( $item->id ){
			$query = 'SELECT field_ordering AS value, label AS text'
			. ' FROM #__ja_form_fields '
			. ' WHERE table_name = "'. $item->table_name .'" AND field_state != "del"'
			. ' ORDER BY field_ordering';
			$order = JHTML::_('list.genericordering',  $query );
			$ordering = JHTML::_('select.genericlist',   $order, 'field_ordering', 'class="inputbox" size="1"', 'value', 'text', intval( $item->field_ordering ) );
		} else
		{
			$ordering = '<input type="hidden" name="field_ordering" value="" />'. JText::_( 'DESCNEWITEMSLAST' );
		}
        $lists['field_ordering'] = $ordering;
		
		/* Field Database table */
		$fieldTableList = JobBoardConstant::getTableList();   
		$selected_table = !$item->id?"" :  $item->table_name;
		
		if(empty($selected_table))
			$selected_table        = $mainframe->getUserStateFromRequest( $option .".jamanagefields.table_name",        'filter_table',        'ja_jobs',    'cmd' );
	    
		$fieldTableListHTML[] = JHTML::_('select.option',  "", JText::_('Select Table') );
	    foreach($fieldTableList as $tableName=>$tableNameLabel){
            $fieldTableListHTML[] = JHTML::_('select.option',  $tableName, $tableNameLabel );
		}
        $lists['table_name'] = JHTML::_('select.genericlist',   $fieldTableListHTML, 'table_name',$disable.'class="inputbox" size="1"', 'value', 'text', $selected_table);
        
        /* Mounse events */
		$mouseList = JobBoardConstant::getEventList();   
		$selected_eventmouse = !$item->id?"" :  $item->eventmouse;		
	    
		$mouseListHTML[] = JHTML::_('select.option',  "", JText::_('Select event') );
	    foreach($mouseList as $value=>$text){
            $mouseListHTML[] = JHTML::_('select.option',  $value, $text );
		}
        $lists['mouseevents'] = JHTML::_('select.genericlist',   $mouseListHTML, 'eventmouse', 'class="inputbox" size="1" style="width:100px"', 'value', 'text', $selected_eventmouse);
        
        /* Others */
        $lists['is_required'] 	= JHTML::_('select.booleanlist', 'is_required', '', $item->is_required);
        $lists['is_on_search'] 	= JHTML::_('select.booleanlist', 'is_on_search', '', $item->is_on_search);
        $lists['is_on_filter'] 	= JHTML::_('select.booleanlist', 'is_on_filter', '', $item->is_on_filter);
        //$lists['is_hidden'] 	= JHTML::_('select.booleanlist', 'is_hidden', '', $item->is_hidden);
        //$lists['frontend_hidden'] 	= JHTML::_('select.booleanlist', 'frontend_hidden', '', $item->frontend_hidden);
        //$lists['is_prefill'] = JHTML::_('select.booleanlist', 'is_prefill', '', $item->is_prefill);
        $lists['is_readonly'] = JHTML::_('select.booleanlist', 'is_readonly', '', $item->is_readonly);
        $lists['is_referred'] = JHTML::_('select.booleanlist', 'is_referred', '', $item->is_referred);
        
        //
		// For preview form
		//
		$pre_task = JRequest::getVar('pretask');
		if ($pre_task=='apply')
			$fieldlist = getEditFieldList($selected_table,'',1, " OR f.field_state = 'ins' OR f.field_state = 'mod'");
		else 
			$fieldlist = getEditFieldList($selected_table);
		
  	    // clean item data
		$put[] = JHTML::_('select.option',  '1', JText::_( 'Yes' ));
		$put[] = JHTML::_('select.option',  '0', JText::_('No' ));
		// If not a new item, trash is not an option
		
		if ( !$item->id) {
			$item->published = 1;
		}
		$published = JHTML::_('select.radiolist',  $put, 'published', '', 'value', 'text', $item->published );
		
		/*if($item->id){
        $query = 'SELECT ordering as value, value as text '
                .' FROM #__ja_salary  '
                .' ORDER BY ordering';
            $order = JHTML::_('list.genericordering',  $query );
            $ordering = JHTML::_('select.genericlist',   $order, 'ordering', 'class="inputbox" size="1"', 'value', 'text', intval( $item->field_ordering ) );
        } else {
            $ordering = '<input type="hidden" name="ordering" value="'. $item->field_ordering .'" />'. JText::_( 'DESCNEWITEMSLAST' );
        }*/
			
       // $this->assignRef('fieldlist', $fieldlist);
  	    $this->assignRef('ordering', $ordering);
  	    $this->assignRef('fieldlist', $fieldlist);
  	    $this->assignRef('option', $option);
  	    $this->assignRef('lists', $lists);
  	    $this->assignRef('published', $published);
  	    $this->assignRef('item', $item);
  	    parent::display($tmpl);
	}
	
	function updtable($step = 1){		        
        $step = JRequest::getVar('step', 0);
        $cid = JRequest::getVar( 'cid', array(0), '', 'array' );
		JArrayHelper::toInteger($cid, array(0));
        $model = $this->getModel();
        $items = $model->getItems(" and (f.field_state='ins' or f.field_state='mod' ) and f.id=".$cid[0]." ");
        $pagination = $model->_pagination;
        $lists = $this->_getViewLists();
        
        $this->assignRef('items', $items);
        $this->assignRef('pagination', $pagination);
        $this->assignRef('lists', $lists);
        $this->assignRef('step', $step);
        
        parent::display();
	}
	
	function required( &$row, $i, $imgY = 'tick.png', $imgX = 'publish_x.png', $prefix='' )
	{
		$img 	= $row->is_required ? $imgY : $imgX;
		$task 	= $row->is_required ? 'required' : 'unrequired';
		$alt 	= $row->is_required ? JText::_( 'Required' ) : JText::_( 'Unrequired' );
		$action = $row->is_required ? JText::_( 'Item unrequired' ) : JText::_( 'Item required' );
				
		$href = '
		<a href="javascript:void(0);" onclick="return listItemTask(\'cb'. $i .'\',\''. $prefix.$task .'\')" title="'. $action .'">
		<img src="images/'. $img .'" border="0" alt="'. $alt .'" /></a>'
		;

		return $href;
	}
   	
    function getExternalCombo($field, $selected="", $is_readonly = 0, $add_empty=1 ){
        if($is_readonly){
            $r = JText::_("&lt;Preview Readonly Value&gt;");    
        } else {            
            $objectHTML[] = JHTML::_('select.option',  "", JText::_("&lt;Preview Value&gt;"));
            $r = JHTML::_('select.genericlist',   $objectHTML, $field->field_name, 'class="inputbox" size="1"', 'value', 'text', $selected);    
        }
        return $r;    
    }
    
    function &_getViewLists()
	{
		global $option, $mainframe;

		$filter_table		= $mainframe->getUserStateFromRequest( $option.".jamanagefields.filter_table",		'filter_table',		'',	'word' );
		$filter_form		= $mainframe->getUserStateFromRequest( $option.".jamanagefields.filter_form",		'filter_form',		'',	'string' );
		$filter_order		= $mainframe->getUserStateFromRequest( $option.".jamanagefields.filter_order",		'filter_order',		'f.field_ordering',	'cmd' );
		$filter_order_Dir	= $mainframe->getUserStateFromRequest( $option.".jamanagefields.filter_order_Dir",	'filter_order_Dir',	'ASC',			'word' );
		$search				= $mainframe->getUserStateFromRequest( $option.".jamanagefields.search",			'search',			'',				'string' );
		$search				= JString::strtolower( $search );
		$lists['option'] = $option;
		// table ordering
		$lists['order_Dir']	= $filter_order_Dir;
		$lists['order']		= $filter_order;

		// search filter
		$lists['search']= $search;
		
		$tableList = JobBoardConstant::getTableList();
		$tablesHTML[] = JHTML::_('select.option',  "", JText::_( 'All Tables' ) );
        foreach($tableList as $tableName=>$tableLabel){
            $tablesHTML[] = JHTML::_('select.option',  $tableName, JText::_($tableLabel) );
		}
        $lists['tables'] = JHTML::_('select.genericlist',   $tablesHTML, 'filter_table', 'class="inputbox" size="1" onchange="document.adminForm.submit();"', 'value', 'text', $filter_table);

        $formslist = JobBoardConstant::getFormList();
        $formsHTML[] = JHTML::_('select.option',  "", JText::_( 'All Form' ) );
        foreach($formslist as $value=>$text){
            $formsHTML[] = JHTML::_('select.option',  $value, JText::_($text) );
		}
        $lists['forms'] = JHTML::_('select.genericlist',   $formsHTML, 'filter_form', 'class="inputbox" size="1" onchange="document.adminForm.submit();"', 'value', 'text', $filter_form);
        
		return $lists;
	}
}
?>